import {carrierBasicInfoGet, dictionaryList, login, menuList, reportGet} from "../../assets/api";
import {getLocal, getToken, setLocal, setToken} from "../../assets/service/auth";
import {vueFilter} from "../../assets/filter";
import router from '../../router'
import md5 from 'md5'
let nationality = {
    carrier:[
        'CARRIER_TYPE', //载体类别
        'COM_NATURE', //机构性质
        'CARRIER_LEVEL', //载体级别
        'REGULATION_TYPE', //管理制度建设
        'FACILITY_TYPE', //创新创业设施和条件
        'CARRIER_ATTACHMENT', //载体附件
        'INDUSTRY_DOMAIN', //行业领域
        'POLICY_PRODUCT_TECHNOLOGY', // 政策产品技术领域
        'POLICY_SERVICE_MARKET_AREA' // 政策服务市场领域
    ],
    policy:[
        'POLICY_TYPE',
        'PUBLISH_TAG',
        'POLICY_RISING_DOMAIN',
        'POLICY_UNSCRAMBLE_TYPE',
        'POLICY_NEWS_TYPE',
        'POLICY_ANNOUNCEMENT_TYPE',
        'PUBLISH_DEPARTMENT',
        'POLICY_TECHNOLOGY_PROJECT_SYSTEM',
        'PUBLISH_AREA',
        'SUBSIDY_TYPE',
    ],
}
const app = {
    state: {
        dictionary: {},
        menuList: [],
        menuData: [],
        menuChecked:[],
        token: null,
        carrierInfo:{},
        advisoryInfo:{},
        newInfo:{},
        isInit:false,
        userInfo: {}
    },
    mutations: {
        GET_NATIONALITY: (state, data) => {
            state.dictionary = data
        },
        SET_MENUDATA: (state, data) => {
            state.menuData = []
            for (let item in data) {
                state.menuData.push(data[item])
            }
            console.log(data, 456)
        },
        SET_MENU_LIST: (state, data) => {
            state.menuList = data
        },
        SET_MENU_CHECKED: (state, data) => {
            console.log(data,888)
            state.menuChecked = data
        },
        SET_TOKEN: (state, data) => {
            state.token = data
        },
        SET_INIT: (state) => {
            state.isInit = true
        },
        LAYOUT:(state)=>{
            state.token = null
            state.menuList = null
            state.menuData = null
        },
        GETCARRIERBASICINFOGET:(state,data)=>{
            state.carrierInfo = data
        },
        SET_USER_INFO:(state,data)=>{
            state.userInfo = data
        }


    },
    actions: {
        setUserInfo({commit},data){
           return commit('SET_USER_INFO',data)
        },
        layout({commit}){
            commit('LAYOUT')
        },
        initCarrier({commit},type) { //初始化载体用户基本信息
            return menuList()
                .then(result => {
                    commit('SET_MENU_LIST', result.result)
                    return result.result
                })
                .then(result=>{
                    let menuList = Object.assign([],result)
                    if(result[0]?.childrenSystemMenuList?.length == 1){
                        menuList = Object.assign([],result[0].childrenSystemMenuList)
                    }
                    return commit('SET_MENU_CHECKED', menuList)
                })
                .then(_ => {
                    return this.dispatch('getNationality', [
                        ...nationality.carrier
                    ])
                })
                .then(_=>{
                    if(type == 'user'){
                        return this.dispatch('getCarrierBasicInfoGet')
                    }
                    return true
                })
                .then(_=>{
                    vueFilter()
                    return commit('SET_INIT')
                })
        },
        
       
        initNomal({commit}) {
            return menuList()
                .then(result => {
                    commit('SET_MENU_LIST', result.result)
                    return result.result
                })
                .then(result=>{
                    let menuList = Object.assign([],result)
                    if(result[0]?.childrenSystemMenuList?.length == 1){
                        menuList = Object.assign([],result[0].childrenSystemMenuList)
                    }
                    return commit('SET_MENU_CHECKED', menuList)
                })
                .then(_ => {
                    return this.dispatch('getNationality', [
                        ...nationality.carrier,
                        ...nationality.policy,
                    ])
                })
                .then(_=>{
                    vueFilter()
                    return commit('SET_INIT')
                })
        },
        setMenuChecked({commit},data){
            let menuList = [data]
            if(data?.childrenSystemMenuList?.length == 1){
                menuList = Object.assign([],data.childrenSystemMenuList)
            }
            return commit('SET_MENU_CHECKED', menuList)
        },
        setToken({commit}){
            commit('SET_TOKEN', getToken())
        },
        getNationality({commit},data) {
            if (getToken()) {
               return dictionaryList({
                    param: data
                })
                    .then(res => {
                       return commit('GET_NATIONALITY', res.result)
                    })
            }
        },
        getCarrierBasicInfoGet({commit}){
            return carrierBasicInfoGet({
                param:0
            })
                .then(result=>{
                    return commit('GETCARRIERBASICINFOGET', result.result)
                })
        },
        setMenuData({commit}, data) {
            commit('SET_MENUDATA', data)
        },
        login({commit},values){
            values.loginPassword = md5(values.loginPassword)
           return login({param:values})
                .then(result=>{
                    commit('SET_TOKEN',result.result)
                    setToken(result.result)
                    setLocal('userInfo',result)
                    router.push('/')
                })
        }
    }
}
export default app
